import { areaList } from '@vant/area-data';
import { config } from '../../utils/config.js'
Page({
  data: {
    areaFromValue: '',
    areaList: areaList,
    showAreaVan: false,
    Adress_city: [],
    showCity_Name: '',
    //  轮播图
    autoplay: true,//循环切换
    interval: 5000,//间隔时间
    duration: 500,
    background: ['red', 'green', 'blue'],
    // 包车模块
    active: 0,
    vanTabList: ['往返', '单程', '包天'],
    dataObj: {
      qidianData: "",
      qidianDatajw: [],
      zhongdianData: '',
      zhongdianDatajw: [],
      chufaTime: '',
      huichengTime: '',
      tujingdian: []
    },
    // 弹出层
    popupShow: false,
    dataTimeActive: '',
    timestamp: new Date().getTime(),
  },
  onChange (e) {
    let { index } = e.detail;
    // wx.showLoading()
    this.setData({ active: index }, () => {
      // wx.hideLoading()
    })
  },
  addLocationress () { // 点击添加途经点
    let { tujingdian } = this.data.dataObj;
    tujingdian.push({ tujingdian: '', tujingdianjw: [] });
    this.setData({ ["dataObj.tujingdian"]: tujingdian })
  },
  showLocation (e) {
    let { code } = e.currentTarget.dataset;
    let that = this;
    let options = {}
    // wx.$getLocation().then(res => {
    let { qidianDatajw } = that.data.dataObj;
    if (qidianDatajw.length > 0) {
      options.latitude = qidianDatajw[0];
      options.longitude = qidianDatajw[1];
    }
    wx.chooseLocation({
      ...options,
      complete: (res) => {
        console.log('complete', res)
        if (res.name == '' || !res.name) return wx.$toast('请确认地址');
        if (res.name) {
          that.setData({
            [code]: res.name,
            [`${code}jw`]: [res.latitude, res.longitude]
          })
        }
      },
      success: (res) => {
        console.log('success', res)
      },
      fail: (res) => {
        console.log('fail', res)
      }
    })
    // })
  },
  onLoad () {
    wx.showLoading();
    this.wxLocation().then(res => {
      wx.hideLoading();
      if (res.message != "Success") return wx.$toast('定位失败');
      let { areaList } = this.data;
      if (areaList && areaList.city_list && Object.values(areaList.city_list).length > 0) {
        Object.values(areaList.city_list).forEach((element, index) => {
          if (element == res.result.address_component.city) {
            this.setData({ areaFromValue: Object.keys(areaList.city_list)[index], areaObj: res.result, showCity_Name: element })
          }
        })
      }
    })
  },
  wxLocation () {
    return new Promise((resolve, reject) => {
      wx.$getLocation().then(res => {
        this.mapGeocoder(res).then(result => {
          resolve(result);
        })
      }, reject)
    })
  },

  //腾讯地图api逆地址转换
  mapGeocoder (latlng) {
    let url = 'map/tencent/ws/geocoder/location';
    let { latitude, longitude } = latlng;
    return new Promise(resolve => {
      wx.$post(2, url, {
        "key": config.mapKey,
        "location": `${latitude},${longitude}`,
        "get_poi": "",
        "poi_options": "",
        "output": "",
        "callback": ""
      }, false, true).then(res => {
        resolve(res)
      })
    })
  },
  // 这里是点击减途经点
  ruduceLocationress (e) {
    let { index } = e.currentTarget.dataset;
    let { tujingdian } = this.data.dataObj;
    let newList = tujingdian.filter((element, idx) => idx !== index);
    this.setData({ ["dataObj.tujingdian"]: newList })
  },
  passingPoints (e) {
    let { index } = e.currentTarget.dataset;
    let options = {}, that = this;
    // wx.$getLocation().then(res => {
    let { tujingdian } = that.data.dataObj;
    if (tujingdian[index].tujingdianjw.length > 0) {
      let data = tujingdian[index].tujingdianjw;
      options.latitude = data[0];
      options.longitude = data[1];
    }
    wx.chooseLocation({
      ...options,
      complete: (res) => {
        console.log('complete ', res)
        if (res.name == '' || !res.name) return wx.$toast('请确认地址')
        if (res.name) {
          let newList = tujingdian.map((element, idx) => {
            if (idx == index) {
              element.tujingdian = res.name;
              element.tujingdianjw = [res.latitude, res.longitude];
            }
            return element
          });
          this.setData({ ["dataObj.tujingdian"]: newList })
        }
      },
    })
    // })

  },
  // 点击弹出层
  tapShowPopup (e) {
    let { code } = e.currentTarget.dataset;
    this.setData({ dataTimeActive: code, popupShow: true })
  },
  // 时间确认
  dataTimeConfirm (e) {
    let timestamp = e.detail; // 时间戳
    this.setData({ [this.data.dataTimeActive]: wx.$util.timestampToTime(timestamp), popupShow: !this.data.popupShow })
  },
  onClose () {
    this.setData({ popupShow: !this.data.popupShow })
  },
  submitData () {
    let quotation_Data = {
      active: this.data.active,
      ...this.data.dataObj,
    }
    console.log('quotation_Data', quotation_Data)
    wx.navigateTo({
      url: '/repairPages/quotation_details/quotation_details',
      success: function (res) {
        res.eventChannel.emit('quotation_Data', quotation_Data)
      }
    })
  },
  areaConfirm (e) {
    Object.values(areaList.city_list).forEach((element, index) => {
      if (element == e.detail.values[1].name) {
        this.setData({ Adress_city: e.detail.values, showCity_Name: e.detail.values[1].name, areaFromValue: Object.keys(areaList.city_list)[index] })
      }
    })
  },
  showAreaVanClick () {
    this.setData({ showAreaVan: true })
  },
  showAreaVanClickCancel () {
    this.setData({ showAreaVan: false })

  }
})
